import {
  RawField,
  WidgetData,
  WidgetOption,
  WidgetType,
  WidgetStyle,
  TableDefault,
  TableValue,
  TableInsertWay,
  LocateType,
  TableItem,
  TableField
} from '@/types'
import WidgetTable from './WidgetTable.vue'
import TableSetting from './TableSetting.vue'
import TableFill from './TableFill.vue'
import { initTextData, rawTextCreator, rawTextGetter } from '../input'

/** 表格 */
export default {
  key: 'table',
  name: '表格',
  type: WidgetType.TABLE,
  style: WidgetStyle.FILL,
  icon: 'fa-icon-widget-table',
  width: 300,
  height: 128,
  component: WidgetTable,
  settingComponent: TableSetting,
  fillingComponent: TableFill,
  settingCustomFillRequirements: true,

  text: {
    maxLength: 300
  },

  /** 初始控件数据 */
  init(widget, data) {
    initTextData(widget, data)

    /** 初始表格配置 */
    const options = (widget.tableOptions = { ...TableDefault, ...data.tableOptions })

    /** 是否横向表格 */
    const { isHorizontalTable: isHorizontal } = widget

    /** 初始字段列表 */
    const fields: TableField[] = (widget.tableFields = [])

    const fieldsLength = Math.max(isHorizontal ? options.cols : options.rows, data.tableFields?.length || 0)

    for (let i = 0; i < fieldsLength; i++) {
      widget.addTableField(i, data.tableFields?.[i])
    }

    let value: TableValue = []

    try {
      value = JSON.parse(data.value as string)
    } catch (e) {
      value = []
    }

    /** 初始表格值 */
    const tData: TableItem[] = (widget.tableData = [])

    const dataLength = Math.max(isHorizontal ? options.rows : options.cols, value.length)

    for (let i = 0; i < dataLength; i++) {
      const item: TableItem = { width: TableDefault.colWidthDefault }

      for (let j = 0, l = fields.length; j < l; j++) {
        item[fields[j].id || ''] = value[i]?.[j] || ''
      }

      tData.push(item)
    }

    widget.tableColWidths = data.tableColWidths || []

    widget.updateTableSize()
  },

  settingValidate(widget) {
    if (widget.tableFields.some(option => !option.label)) {
      return '请设置表头名称'
    }
  },

  usingValidate(widget) {
    return widget.tableFillError?.error
  },

  hasFillValue(widget) {
    return !!widget.tableValue.flat().join('')
  },

  labelTips(widget) {
    const {
      required,
      tableOptions: { requiredCount },
      isHorizontalTable
    } = widget

    return required && requiredCount ? `至少填写${requiredCount}${isHorizontalTable ? '行' : '列'}` : ''
  },

  rawWidgetCreator(widgetData, data) {
    const { tableOptions, tableFields, colWidthPercent: tableColWidths } = data

    rawTextCreator(widgetData, data)

    return Object.assign(widgetData, {
      tableOptions,
      tableFields,
      tableColWidths
    } as WidgetData)
  },

  rawFieldGetter(widget, data) {
    const { x, y, tableOptions, tableFields, tableColWidths: colWidthPercent } = widget

    rawTextGetter(widget, data)

    return Object.assign(data, {
      x,
      y,
      locateType: LocateType.LEFT_TOP,
      tableOptions,
      tableFields,
      colWidthPercent,
      tableInsertWay: TableInsertWay.AXIS
    } as RawField)
  }
} as WidgetOption
